package com.nbgroup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nbgroup.mapper.CourseMapper;
import com.nbgroup.pojo.Course;
import com.nbgroup.pojo.common.PageBean;
import com.nbgroup.pojo.dto.PageCourseDTO;
import com.nbgroup.service.CourseService;
import com.nbgroup.util.RandomUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class CourseServiceImpl implements CourseService {
private final CourseMapper courseMapper;
private final RandomUtil randomUtil;

    @Override
    public PageBean coursePageQuery(PageCourseDTO pageCourseDTO) {
        pageCourseDTO.check();
        PageHelper.startPage(pageCourseDTO.getPageNum(),pageCourseDTO.getPageSize());
        Page<Course> courses =courseMapper.coursePageQuery(pageCourseDTO);
        PageBean pageBean = new PageBean();
        pageBean.setTotal(courses.getTotal());
        pageBean.setRows(courses.getResult());
        return pageBean;

    }

    @Override
    public void addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        //随机8位编号，字母小写加数字
        String randomNumber = randomUtil.getSmallRandomNumber();
        course.setCode(randomNumber);
        courseMapper.addCourse(course);
    }

    @Override
    public Course queryCourse(Long id) {
      Course course = courseMapper.queryCourse(id);
        return course;
    }

    @Override
    public void updateCourse(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateCourse(course);
    }

    @Override
    public void deleteCourse(List<Long> ids) {
        courseMapper.deleteCourse(ids);
    }
}
